package com.py.credit.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.druid.util.Base64;
import com.py.credit.ai.client.IAipFace;
import com.py.credit.ai.client.IAipOrc;
import com.py.credit.ai.constant.AipFaceConstant;
import com.py.credit.ai.res.IdCardResult;
import com.py.credit.ai.res.LicenseResult;
import com.py.framework.core.rest.support.RestServiceResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api
@RestController
@RequestMapping("/aip")
public class AiFaceController {

	@Value("${ai.face.group}")
	private String groupId;

	@Autowired
	IAipFace aipFaceManager;

	@Autowired
	IAipOrc aipOrcManager;

	@ApiOperation(value = "人脸检测")
	@RequestMapping(value = "/face/isFace.json", method = RequestMethod.POST)
	public RestServiceResult<Boolean> isFace(@RequestParam(required = true, value = "image") MultipartFile image)
			throws IOException {

		return new RestServiceResult<Boolean>(
				aipFaceManager.detect(Base64.byteArrayToBase64(image.getBytes()), AipFaceConstant.IMAGE_TYPE_BASE64));
	}

	@ApiOperation(value = "身份证识别图像数据，最大4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式")
	@RequestMapping(value = "/orc/verifyIdCard.json", method = RequestMethod.POST)
	public RestServiceResult<IdCardResult> idcard(@RequestParam(required = true, value = "image") MultipartFile image,
			@RequestParam(required = true, value = "idCardSide") String idCardSide) throws IOException {

		IdCardResult idcard = aipOrcManager.idcard(image.getBytes(), idCardSide);

		return new RestServiceResult<IdCardResult>(idcard);

	}

	@ApiOperation(value = "经营许可证识别,图像数据,最大4M4M，最短边至少15px，最长边最大4096px,支持jpg/png/bmp格式")
	@RequestMapping(value = "/orc/bussinessLisencess.json", method = RequestMethod.POST)
	public RestServiceResult<LicenseResult> bussinessLisencess(
			@RequestParam(required = true, value = "image") MultipartFile image) throws IOException {

		LicenseResult businessLicense = aipOrcManager.businessLicense(image.getBytes());

		return new RestServiceResult<LicenseResult>(businessLicense);

	}

}
